using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WP.Controls;
namespace WP.Controls
{
    public class TallyTextColumn : DataGridViewColumn
    {

        private TallyText.TallyType m_TallyStlye = TallyText.TallyType.Pc_Len;
        private decimal m_FixLen = 0;
        private decimal m_FixWidth = 0;
        private int m_FixPc = 0;

        public TallyText.TallyType TallyStlye
        {
            get
            {
                return m_TallyStlye;
            }
            set
            {
                m_TallyStlye = value;
            }
        }

        public decimal FixLen
        {
            get
            {
                return m_FixLen;
            }
            set
            {
                m_FixLen = value;

            }
        }

        public decimal FixWidth
        {
            get
            {
                return m_FixWidth;
            }
            set
            {
                m_FixWidth = value;
            }
        }
        public int FixPc
        {
            get
            {
                return m_FixPc;
            }
            set
            {
                m_FixPc = value;
            }
        }

        public TallyTextColumn(TallyText.TallyType sType,int tallyFixPc, decimal tallyFixLength, decimal tallyFixWidth)
            : base(new TallyTextCell())
        {
            m_TallyStlye = sType;
            m_FixPc = tallyFixPc;
            m_FixLen = tallyFixLength;
            m_FixWidth = tallyFixWidth;
        }
        public TallyTextColumn()
            : base(new TallyTextCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a TallyCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(TallyTextCell)))
                {
                    throw new InvalidCastException("Must be a TallyCell");
                }
                base.CellTemplate = value;
            }

        }
    }

    public class TallyTextCell : DataGridViewTextBoxCell
    {

         public TallyTextCell()
            : base() 
        {

        }


        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);
            TallyTextEditingControl ctl =
                DataGridView.EditingControl as TallyTextEditingControl ;


            ctl.TallyStlye = ((TallyTextColumn)this.DataGridView.Columns[this.ColumnIndex]).TallyStlye;
            ctl.TallyFixLength = ((TallyTextColumn)this.DataGridView.Columns[this.ColumnIndex]).FixLen;
            ctl.TallyFixWidth = ((TallyTextColumn)this.DataGridView.Columns[this.ColumnIndex]).FixWidth;
            ctl.TallyFixPc = ((TallyTextColumn)this.DataGridView.Columns[this.ColumnIndex]).FixPc;

            if (this.RowIndex >= 0 && this.ColumnIndex >= 0)
            {
                ctl.Value = (string)this.Value;
            }
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that TallyCell uses.
                return typeof(TallyTextEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that TallyCell contains.
                return typeof(string);
            }
        }


        protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
        {

        }

        protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
        {
            if (e.KeyChar == (char)Keys.Return)
                e.Handled = true;
        }

    }

    class TallyTextEditingControl : TallyText, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public TallyTextEditingControl()
        {
            //this.Format = DateTimePickerFormat.Short;
        }



        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Value;
            }
            set
            {
                String newValue = value as String;
                if (newValue != null)
                {
                    this.Value = newValue;
                }
            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            //this.Font = dataGridViewCellStyle.Font;
            //this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            //this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {

            // Let the DateTimePicker handle the keys listed.
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.Return:
                case Keys.PageUp:
                    this.Refresh();
                    return true;
                default:
                    return false;
            }
        }

        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        protected override void OnValueChanged(TallyEventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell
            // have changed.
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            this.dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
            base.OnValueChanged(eventargs);
         //   this.dataGridView.RefreshEdit();  
        }
    }
}
